package com.lihui.exercises;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2022/10/12  9:52
 */
public class Exe_LinkedList02 {
    private static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    //回文链表
    public boolean chkPalindrome(ListNode head) {
        if(head == null){
            return false;
        }

        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode current = slow.next;
        while(current != null){
            ListNode nextNode = current.next;
            current.next = slow;
            slow = current;
            current = nextNode;
        }
        while(head != slow){
            if(head.val != slow.val){
                return false;
            }
            slow = slow.next;
            head = head.next;
            if(head.next == slow){
                return true;
            }
        }
        return true;
    }
    //相交链表
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        int lenA = 0;
        int lenB = 0;
        ListNode headLong = headA;
        ListNode headShort = headB;
        while(headLong != null){
            lenA++;
            headLong = headLong.next;
        }
        while(headShort != null){
            lenB++;
            headShort = headShort.next;
        }
        int len = lenA - lenB;
        if(len < 0){
            len = lenB - lenA;
            headLong = headB;
            headShort = headA;
        }else{
            headLong = headA;
            headShort = headB;
        }
        while(len > 0){
            headLong = headLong.next;
            len--;
        }
        while(headLong != headShort){
            headLong = headLong.next;
            headShort = headShort.next;
        }
        return headLong;
    }
    //是否有环
    public boolean hasCycle(ListNode head) {
        if(head == null){
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }

    //是否有环 ，有环输出入环节点
    public ListNode detectCycle(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return null;
        }
        fast = head;
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
